<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\Factory\Mssql\Region AS FactoryRegion;       //电厂数据模型
use App\Models\Headquarters\Region AS HQRegion;   //总部数据模型
use Exception;
use PDOException;
use Log;

class RegionDataJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $factory_conn; //电厂连接
    protected $remote_conn;   //总部连接
    protected $factory_data_table; //电厂保存的原始数据集合
    protected $remote_data_table; //总部保存的格式化后的数据集合
    public $tries = 1;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($params=null)
    {
        $this->factory_conn = $params && isset($params['factory_conn']) ? $params['factory_conn'] : '';
        $this->remote_conn = $params && isset($params['remote_conn']) ? $params['remote_conn'] : '';
        $this->factory_data_table = $params && isset($params['factory_data_table']) ? $params['factory_data_table'] : '';
        $this->remote_data_table = $params && isset($params['remote_data_table']) ? $params['remote_data_table'] : '';
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try{
            $this->saveData();
        }
        catch(Exception $ex){
            Log::info('JOB异常');
            Log::info(var_export($ex, true));
        }
    }

    protected function saveData(){
        try{
            $factoryRegion = (new FactoryRegion())->setConnection($this->factory_conn)->setTable($this->factory_data_table);  //连接电厂内部数据库
            $hQRegion = (new HQRegion())->setConnection($this->remote_conn)->setTable($this->remote_data_table);      //连接本地数据库表
            $datalist = $factoryRegion->findAll();
            $params = [];
            if($datalist && count($datalist) > 0){
                foreach ($datalist as $key => $item) {
                    $hq_row = $hQRegion->findRowById($item->ID);
                    if(!$hq_row){
                        //不存在则插入
                        $params[] = array(
                            'id' => $item->ID,
                            'RegionName' => $item->RegionName,
                            'uploadstate'=> $item->uploadstate,
                            'RegionCode'=> $item->RegionCode,
                            'flag'=> 0
                        );
                    }
                }
            }

            if($params && count($params) > 0){
                foreach($params as $item){
                    $id = $item['id'];
                    unset($item['id']);
                    $hQRegion->updateOrCreate([
                        "id" => $id
                    ], $item);
                }
                Log::info('地磅数据库数据Region同步成功'.count($params).'条');
            }
            else{
                Log::info('地磅数据库Region没有数据插入');
            }
        }
        catch(PDOException $ex){
            Log::info('连接电厂地磅数据库异常');
            Log::info(var_export($ex, true));
        }
    }
}
